// 定义简历响应数据类型
interface ResumeResponse {
    fileName: string;
    fileType: string;
    base64Data: string;
  }
  
  /**
   * 将 Base64 转换为 Blob 对象
   */
  export function base64ToBlob(base64: string, contentType: string): Blob {
    const byteCharacters = atob(base64);
    const byteArrays = [];
  
    for (let offset = 0; offset < byteCharacters.length; offset += 512) {
      const slice = byteCharacters.slice(offset, offset + 512);
      const byteNumbers = new Array(slice.length);
  
      for (let i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }
  
      byteArrays.push(new Uint8Array(byteNumbers));
    }
  
    return new Blob(byteArrays, { type: contentType });
  }
  
  /**
   * 处理简历预览
   */
  export function handleResumePreview(resumeData: ResumeResponse): {
    previewUrl: string;
    fileName: string;
    fileType: string;
  } {
    const { fileName, fileType, base64Data } = resumeData;
    const blob = base64ToBlob(base64Data, fileType);
    const previewUrl = URL.createObjectURL(blob);
  
    return {
      previewUrl,
      fileName,
      fileType,
    };
  }
  
  /**
   * 下载文件
   */
  export function downloadFile(url: string, fileName: string): void {
    const link = document.createElement('a');
    link.href = url;
    link.download = fileName || 'download';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
  
  /**
   * 清理预览URL
   */
  export function cleanupPreviewUrl(url: string): void {
    URL.revokeObjectURL(url);
  }